package com.woniuxy.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.woniuxy.entity.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.entity.Wallet;
import com.woniuxy.service.CoachService;
import com.woniuxy.service.UserService;
import com.woniuxy.util.CommonUtil;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	UserService userService;
	@Autowired
	CoachService coachService;

	/**
	 * @author 曾令忠
	 */
	@RequestMapping("/nowUser")
	@ResponseBody
	public ResponseEntity<User> getNowUser(){
		ResponseEntity<User> re = new ResponseEntity<User>();
		User user = CommonUtil.getCurrentUser();
		re.setData(user);
		re.setCode(1);
		return re;
	}
	/**
	 * @author 曾令忠
	 */
	@RequestMapping("/nearUser")
	@ResponseBody
	public ResponseEntity<Map<String, List<User>>> getNearUsers(){
		ResponseEntity<Map<String, List<User>>> re = new ResponseEntity<>();
		Map<String, List<User>> map = new HashMap<>();
		//判断用户
		try {
			User nowUser = CommonUtil.getCurrentUser();
//		User nowUser = new User().setLatitude(22.216).setLongitude(22.219).setId(0).setRole("2");
			User select = new User().setLatitude(nowUser.getLatitude())
					.setLongitude(nowUser.getLongitude())
					.setId(nowUser.getId());
			if("1".equals(nowUser.getRole())) {
				select.setRole("2");
				List<User> coaches = userService.getNearUser(select, 2);
				map.put("coach", coaches);
				select.setRole("3");
				List<User> gyms = userService.getNearUser(select, 2);
				map.put("gym", gyms);
			}else if("2".equals(nowUser.getRole())) {
				select.setRole("1");
				List<User> students = userService.getNearUser(select, 2);
				map.put("student", students);
				select.setRole("3");
				List<User> gyms = userService.getNearUser(select, 2);
				map.put("gym", gyms);
			}else{
				select.setRole("2");
				List<User> coachs = userService.getNearUser(select, 2);
				map.put("coach", coachs);
				select.setRole("1");
				List<User> students = userService.getNearUser(select, 2);
				map.put("student", students);
			}
		} catch (Exception e) {
			re.setCode(2);
			e.printStackTrace();
			return re;
		}
		re.setCode(1);
		re.setData(map);
		return re;
	}
	
	/**
	 * @author 曾令忠
	 */
	@RequestMapping("/balance")
	@ResponseBody
	public ResponseEntity<Wallet> getWallet(){
		ResponseEntity<Wallet> re = new ResponseEntity<>();
		try {
			User nowUser = CommonUtil.getCurrentUser();
			Wallet w = userService.getMyWallet(nowUser.getId());
			re.setData(w);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}

	/*
	 * @author 幸琦林
	 *
	 */
	@RequestMapping(value = "/getCurrentUser", method = RequestMethod.GET)
	@ResponseBody
	public User getCurrentUser() {
		return CommonUtil.getCurrentUser();
	}

	//	通过userId查询附近的目标
	/*
	 * @author 幸琦林
	 *
	 */
	@RequestMapping(value = "/getShowUser", method = RequestMethod.GET)
	@ResponseBody
	public List<User> getShowUser(int userId, int roleId) {
		List<User> nears = new LinkedList<>();
		if (roleId == 1) {
			List<User> coaches = getCoaches(userId);
			List<User> gyms = getGyms(userId);
			nears.addAll(coaches);
			nears.addAll(gyms);
		} else if (roleId == 2) {
			List<User> users = getUsers(userId);
			List<User> gyms = getGyms(userId);
			nears.addAll(users);
			nears.addAll(gyms);
		} else {
			List<User> coaches = getCoaches(userId);
			List<User> users = getUsers(userId);
			nears.addAll(users);
			nears.addAll(coaches);
		}
		return nears;
	}

	/*
	 * @author 幸琦林
	 *
	 */
	@RequestMapping(value = "/getUsers", method = RequestMethod.GET)
	@ResponseBody
	public List<User> getUsers(int userId) {
		User user = userService.findUserById(userId);
		user.setRole("1");
		List<User> nearAllUser = userService.getNearAllUser(user);
		return nearAllUser;
	}

	/*
	 * @author 幸琦林
	 *
	 */
	@RequestMapping(value = "/getCoaches", method = RequestMethod.GET)
	@ResponseBody
	public List<User> getCoaches(int userId) {
		User user = userService.findUserById(userId);
		user.setRole("2");
		List<User> nearAllUser = userService.getNearAllUser(user);
		return nearAllUser;
	}

	/*
	 * @author 幸琦林
	 *
	 */
	@RequestMapping(value = "/getGyms", method = RequestMethod.GET)
	@ResponseBody
	public List<User> getGyms(int userId) {
		User user = userService.findUserById(userId);
		user.setRole("3");
		List<User> nearAllUser = userService.getNearAllUser(user);
		return nearAllUser;
	}

	@RequestMapping("/confirmUpdate")
	@ResponseBody
	public ResponseEntity<Void> confirmUpdate(String pwd){
		ResponseEntity<Void> re  = new ResponseEntity<Void>();
		try {
			User currentUser = CommonUtil.getCurrentUser();
			pwd = CommonUtil.getMd5Pwd(currentUser.getAccount(), pwd);
			userService.updatePwd(currentUser.getId(),pwd);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	/**
	 * @author 曾令忠
	 */
	@RequestMapping("/setRole")
	@ResponseBody
	public ResponseEntity<Void> setRole(String role){
		ResponseEntity<Void> re = new ResponseEntity<Void>();
		User user = CommonUtil.getCurrentUser();
		try {
			user.getRoles().add(new SysRole().setIdRole(Long.parseLong(role) + 1));
			user.setRole(role);
			userService.setRole(user.getId(), role);
			re.setCode(1);
		} catch (Exception e) {
			e.printStackTrace();
			re.setCode(2);
		}
		return re;
	}
	
	/**
	 * @author 曾令忠
	 */
	@RequestMapping("/hasrole")
	@ResponseBody
	public String hasRole() {
		User user = CommonUtil.getCurrentUser();
		User nowUser = coachService.getUserById(user.getId());
		if("1".equals(nowUser.getRole())) {
			return "student";
		}
		if("2".equals(nowUser.getRole())) {
			return "coach";
		}
		if("3".equals(nowUser.getRole())) {
			return "gym";
		}
		return "";
	}
}
